// 如需调试，可以修改服务端ip地址。8887端口如果有其他服务可以更换端口。

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <fcntl.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ServerPort  8887
#define BUFFER_SIZE 1024

int main()
{
    int sockClient = socket(AF_INET,SOCK_STREAM, 0);
    
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(ServerPort);
    servaddr.sin_addr.s_addr = inet_addr("192.168.0.107");
    
    if (connect(sockClient, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("Connect error");
        exit(1);
    }

    char sendbuf[BUFFER_SIZE];
    char recvbuf[BUFFER_SIZE];
    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
    {
        send(sockClient, sendbuf, strlen(sendbuf),0);
        if(strcmp(sendbuf,"exit\n")==0)
            break;
        recv(sockClient, recvbuf, sizeof(recvbuf),0);
        fputs(recvbuf, stdout);

        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
    }

    close(sockClient);
    return 0;
}